package com.tfswue.nyf.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一响应工具类
 * @author daygods
 * @date 2025-07-10
 */
public class ResponseUtil {
    
    private static final String SUCCESS_CODE = "200";
    private static final String ERROR_CODE = "500";
    private static final String SUCCESS_MESSAGE = "操作成功";
    private static final String ERROR_MESSAGE = "操作失败";
    
    /**
     * 成功响应
     */
    public static Map<String, Object> success() {
        return success(null);
    }
    
    /**
     * 成功响应带数据
     */
    public static Map<String, Object> success(Object data) {
        return success(SUCCESS_MESSAGE, data);
    }
    
    /**
     * 成功响应带消息和数据
     */
    public static Map<String, Object> success(String message, Object data) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", SUCCESS_CODE);
        result.put("message", message);
        result.put("data", data);
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
    
    /**
     * 失败响应
     */
    public static Map<String, Object> error() {
        return error(ERROR_MESSAGE);
    }
    
    /**
     * 失败响应带消息
     */
    public static Map<String, Object> error(String message) {
        return error(ERROR_CODE, message);
    }
    
    /**
     * 失败响应带错误码和消息
     */
    public static Map<String, Object> error(String code, String message) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", code);
        result.put("message", message);
        result.put("data", null);
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
    
    /**
     * 分页响应
     */
    public static Map<String, Object> page(Object data, long total, int page, int size) {
        Map<String, Object> pageInfo = new HashMap<>();
        pageInfo.put("list", data);
        pageInfo.put("total", total);
        pageInfo.put("page", page);
        pageInfo.put("size", size);
        pageInfo.put("pages", (total + size - 1) / size);
        
        return success("查询成功", pageInfo);
    }
}